package com.gitee.candy.encry;

import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptMD5Utils {

    public static String encrypt(String str) {
        byte[] digest = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            digest = md5.digest(str.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //16是表示转换为16进制数
        String md5Str = new BigInteger(1, digest).toString(16);
        return md5Str;
    }

    public static String encryptJdk(String str) {
        return DigestUtils.md5DigestAsHex(str.getBytes());
    }


    public static void main(String[] args) {
        System.out.println(encrypt("宝剑分从磨砺出，梅花香自苦寒来"));
        System.out.println(encryptJdk("宝剑分从磨砺出，梅花香自苦寒来"));
    }
}
